#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
导数与统计流程：
1、根据查询条件从mongo得到数据集
2、遍历数据集，筛选符合条件数据进行导出或者统计
'''

import json
import sys
sys.path.append('/mnt/moonbrook/Moonbrook')

import subprocess
import os
import re
import psutil
import time
import requests
from copy import deepcopy
from datetime import datetime, timedelta
from pybloom_live import ScalableBloomFilter
from secret import batch_encrypt_decrypt_dict
from aes import contact_aes, name_aes
from apscheduler.schedulers.blocking import BlockingScheduler
from datastore.mongodb import MongoDBClientWrapper

from base import ExportAndTjBase
from utils import Utils
from models import MongoQueryConfig, RobotConfig
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')

class Field:

    def __init__(self, name='', annotation="", prioties=-1, is_dup=False):
        self.name = name  # str 统计的键名
        self.annotation = annotation  # str 键名对应的含义
        self._is_dup = is_dup  # bool，统计时是否去重
        self.prioties = prioties  # int  输出的优先级，越小越优先输出

    def __lt__(self, other):
        return self.prioties < other.prioties

    @property
    def is_dup(self):
        return self._is_dup

    @property
    def title(self):
        return self.annotation


    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

class ResultConfig:
    all_table_count = Field('all_table_count', annotation="库内联系方式总量",prioties=1)  # 库内联系方式总量(未去重)
    all_update = Field('all_update', annotation='库内周更新量')
    all_add = Field('all_add', annotation='库内周新增量')
    all_abs_add = Field('all_abs_add', annotation='库内绝对新增', is_dup=True)  # (contactChannelList为1的)

    mobile_update = Field('mobile_update', annotation='库内手机周更新数量',prioties=11)
    mobile_update_dup = Field('mobile_update_dup', annotation='手机周更新企业数量', is_dup=True,prioties=12)
    mobile_add = Field('mobile_add', annotation='库内手机周新增量',prioties=13)
    mobile_add_dup = Field('mobile_add_dup', annotation='库内手机周新增企业量', is_dup=True,prioties=14)
    mobile_abs_add = Field('mobile_abs_add', annotation='库内手机周绝对新增量',prioties=15)
    mobile_abs_add_ent_dup = Field('mobile_abs_add_ent_dup', annotation='库内手机周绝对新增企业量',
                                   is_dup=True,prioties=16)  # 周绝对新增企业数量 todo 可能存在性能问题

    phone_update = Field('phone_update', annotation='库内固话周更新数量',prioties=26)
    phone_update_dup = Field('phone_update_dup', annotation='固话周更新企业数量', is_dup=True,prioties=27)
    phone_add = Field('phone_add', annotation='库内固话周新增量',prioties=28)
    phone_add_dup = Field('phone_add_dup', annotation='库内固话周新增企业量', is_dup=True,prioties=29)
    phone_abs_add = Field('phone_abs_add', annotation='库内固话周绝对新增量',prioties=30)
    phone_abs_add_ent_dup = Field('phone_abs_add_ent_dup', annotation='库内固话周绝对新增企业量',
                                  is_dup=True,prioties=31)  # 周绝对新增企业数量 todo 可能存在性能问题

    email_update = Field('email_update', annotation='库内邮箱周更新数量',prioties=41)
    email_update_dup = Field('email_update_dup', annotation='邮箱周更新企业数量', is_dup=True,prioties=42)
    email_add = Field('email_add', annotation='库内邮箱周新增量',prioties=43)
    email_add_dup = Field('email_add_dup', annotation='库内邮箱周新增企业量', is_dup=True,prioties=44)
    email_abs_add = Field('email_abs_add', annotation='库内邮箱周绝对新增量',prioties=45)
    email_abs_add_ent_dup = Field('email_abs_add_ent_dup', annotation='库内固话周绝对新增企业量',
                                  is_dup=True,prioties=46)  # 周绝对新增企业数量 todo 可能存在性能问题

    # -------------新增--------
    # 可能存在手机号重复的情况，即多家企业拥有同一个手机号，以后再对手机号进行去重
    one_month_mobile_update = Field("one_month_mobile_update", annotation='近一个月手机更新数量',prioties=5)
    one_month_mobile_update_dup = Field("one_month_mobile_update_dup", annotation='近一个月手机更新企业数量', is_dup=True,prioties=6)
    one_month_phone_update = Field("one_month_phone_update", annotation='近一个月固话更新数量',prioties=20)
    one_month_phone_update_dup = Field("one_month_phone_update_dup", annotation='近一个月固话更新企业数量', is_dup=True,prioties=21)
    one_month_email_update = Field("one_month_email_update", annotation='近一个月邮箱更新数量',prioties=35)
    one_month_email_update_dup = Field("one_month_email_update_dup", annotation='近一个月邮箱更新企业数量', is_dup=True,prioties=36)

    two_month_mobile_update = Field("two_month_mobile_update", annotation='近2个月手机更新数量',prioties=7)
    two_month_mobile_update_dup = Field("two_month_mobile_update_dup", annotation='近2个月手机更新企业数量', is_dup=True,prioties=8)
    two_month_phone_update = Field("two_month_phone_update", annotation='近2个月固话更新数量',prioties=22)
    two_month_phone_update_dup = Field("two_month_phone_update_dup", annotation='近2个月固话更新企业数量', is_dup=True,prioties=23)

    two_month_email_update = Field("two_month_email_update", annotation='近2个月邮箱更新数量',prioties=37)
    two_month_email_update_dup = Field("two_month_email_update_dup", annotation='近2个月邮箱更新企业数量', is_dup=True,prioties=38)

    three_month_mobile_update = Field("three_month_mobile_update", annotation='近3个月手机更新数量',prioties=9)
    three_month_mobile_update_dup = Field("three_month_mobile_update_dup", annotation='近3个月手机更新企业数量', is_dup=True,prioties=10)
    three_month_phone_update = Field("three_month_phone_update", annotation='近3个月固话更新数量',prioties=24)
    three_month_phone_update_dup = Field("three_month_phone_update_dup", annotation='近3个月固话更新企业数量', is_dup=True,prioties=25)

    three_month_email_update = Field("three_month_email_update", annotation='近3个月邮箱更新数量',prioties=39)
    three_month_email_update_dup = Field("three_month_email_update_dup", annotation='近3个月邮箱更新企业数量', is_dup=True,prioties=40)
    # 在营总量统计
    zy_enterprise_count = Field("zy_enterprise_count", annotation='在营企业的数量', is_dup=True,prioties=47)
    zy_enterprise_mobile_count = Field("zy_enterprise_mobile_count", annotation='在营企业的手机条数',prioties=48)
    week_zy_enterprise_mobile_update = Field("week_zy_enterprise_mobile_update", annotation='周更新在营企业的手机条数',prioties=52)
    week_zy_enterprise_mobile_add = Field("week_zy_enterprise_mobile_add", annotation='周新增在营企业的手机条数',prioties=53)
    week_zy_enterprise_mobile_abs_add = Field("week_zy_enterprise_mobile_abs_add", annotation='周绝对新增在营企业的手机量',
                                              is_dup=True,prioties=54)  # 在营企业的手机数量周绝对新增
    one_month_zy_enterprise_mobile_update = Field("one_month_zy_enterprise_mobile_update",
                                                  annotation='近一个月更新在营企业的手机条数',prioties=49)  # 在营企业的手机数量近一个月更新
    two_month_zy_enterprise_mobile_update = Field("two_month_zy_enterprise_mobile_update", annotation='近2个月更新在营企业的手机条数',prioties=50)
    three_month_zy_enterprise_mobile_update = Field("three_month_zy_enterprise_mobile_update",
                                                    annotation='近3个月更新在营企业的手机条数',prioties=51)

    zy_enterprise_phone_count = Field("zy_enterprise_phone_count", annotation='在营企业的固话条数')
    week_zy_enterprise_phone_update = Field("week_zy_enterprise_phone_update", annotation='周更新在营企业的固话条数')
    week_zy_enterprise_phone_add = Field("week_zy_enterprise_phone_add", annotation='周新增在营企业的固话条数')
    week_zy_enterprise_phone_abs_add = Field("week_zy_enterprise_phone_abs_add", annotation='周绝对新增在营企业的固话量',
                                             is_dup=True)  # 在营企业的固话数量周绝对新增
    one_month_zy_enterprise_phone_update = Field("one_month_zy_enterprise_phone_update",
                                                 annotation='近一个月更新在营企业的固话条数')  # 在营企业的固话数量近一个月更新
    two_month_zy_enterprise_phone_update = Field("two_month_zy_enterprise_phone_update",
                                                 annotation='近2个月更新在营企业的固话条数')
    three_month_zy_enterprise_phone_update = Field("three_month_zy_enterprise_phone_update",
                                                   annotation='近3个月更新在营企业的固话条数')

    zy_enterprise_email_count = Field("zy_enterprise_email_count", annotation='在营企业的邮箱条数')
    week_zy_enterprise_email_update = Field("week_zy_enterprise_email_update", annotation='周更新在营企业的邮箱条数')
    week_zy_enterprise_email_add = Field("week_zy_enterprise_email_add", annotation='周新增在营企业的邮箱条数')
    week_zy_enterprise_email_abs_add = Field("week_zy_enterprise_email_abs_add", annotation='周绝对新增在营企业的邮箱量',
                                             is_dup=True)  # 在营企业的邮箱数量周绝对新增
    one_month_zy_enterprise_email_update = Field("one_month_zy_enterprise_email_update",
                                                 annotation='近一个月更新在营企业的邮箱条数')  # 在营企业的邮箱数量近一个月更新
    two_month_zy_enterprise_email_update = Field("two_month_zy_enterprise_email_update",
                                                 annotation='近2个月更新在营企业的邮箱条数')
    three_month_zy_enterprise_email_update = Field("three_month_zy_enterprise_email_update",
                                                   annotation='近3个月更新在营企业的邮箱条数')

    # 全表数量统计
    table_mobile_count = Field("table_mobile_count", annotation='库内手机条数总量',prioties=2)
    table_mobile_count_dup = Field("table_mobile_count_dup", annotation='库内手机号总量', is_dup=True,prioties=3)
    table_mobile_count_ent_dup = Field('table_mobile_count_ent_dup', annotation='库内手机映射企业数量', is_dup=True,prioties=4)
    table_phone_count = Field("table_phone_count", annotation='库内固话条数总量',prioties=17)
    table_phone_count_dup = Field('table_phone_count_dup', annotation='库内固话号码总量', is_dup=True,prioties=18)
    table_phone_count_ent_dup = Field('table_phone_count_ent_dup', annotation='库内固话映射企业数量', is_dup=True,prioties=19)
    table_email_count = Field("table_email_count", annotation='库内邮箱条数总量',prioties=32)
    table_email_count_dup = Field('table_email_count_dup', annotation='库内邮箱号总量', is_dup=True,prioties=33)
    table_email_count_ent_dup = Field('table_email_count_ent_dup', annotation='库内邮箱映射企业数量', is_dup=True,prioties=34)

    # save_result_dic可以放入类变量中，避免传参调用导致逻辑混乱

class ExportAndTj(ExportAndTjBase):
    source_dic = {
        'qcc': ['qcc'],
        'b2b': ['jc001', '027zpw', 'cnlist', 'hqew', 'byxx', 'N127', 'mmfj', 'tonbao', '21food', 'jx188', 'tpy888',
                'qiugouxinxi', 'guidechem',
                'made-in-china', 'tfsb', 'zhaozhaoqu', 'czvv', '52bjw', 'huohuli', '3g.ganji', 'booksir', '78gk',
                'chadedao', 'baixing', '11467', 'sonhoo', 'd17', 'alibaba', '51sole', 'qydsj', 'cn.made-in-china',
                'm.cpooo', 'qymgc', 'yellowurl', 'makepolo', 'cnlinfo', 'bestb2b', '51g3', 'jiqimao', 'qiyetong',
                'cn5135', 'xianjichina', 'company.biz72', 'chem17', 'bzjw', 'qy6', 'globalimporter', 'c-c', 'abiz',
                'sooshong', 'jqw', 'wmb2b', 'eb80', '1024sj', 'yiwugo', 'bizrobot', 'qichamao', 'herostart',
                'jiancai365', 'baidu_alibaba', 'chinafastenerinfo', 'b2b168', 'globrand', '71ab', 'vooec',
                'tecenet', '912688', 'ic98', 'huangye88', '258', 'huijucn', 'qqhyw', 'youboy', 'texindex', 'qiyeku',
                'hunt007', 'gtobal', 'jinanfa', 'b2b.baidu', 'hc360', 'qth58', '007swz', 'ebdoor', 'taojindi',
                'biz72', 'mobile.gongchang', 'bmlink', '7wsh', '99114', '58', 'company.dzsc', 'ilsnc', u'atobo',
                '1688', 'ccn', 'trustexporter', 'sg560', 'guihenongye', 'coovee', 'zk71', 'gbs', 'zj123'],
        'bidding': ['ccgp', 'ggzy', 'zhaotx', 'ggzyfw.beijing', 'ggzy.gz', 'ggzy.sz', 'qudaobao', 'zfcg.sh',
                    'gdgpo', 'zfcg.czt.zj', 'ccgp-sichuan.gov', 'jxsggzy', 'ccgp-shandong', 'ccgp-hainan',
                    'sxzwfw.gov', 'ccgp-chongqing', 'ccgp-tianjin', 'ccgp-shaanxi', 'ccgp-jiangsu', 'ccgp-guangxi',
                    'hngp', 'ccgp-hunan', 'ccgp-liaoning', 'qhggzyjy', 'nxggzyjy', 'hbggzyfwpt', 'ccgp-yunnan',
                    'nmgp', 'xzzbtb', 'ggzyfw.fujian', 'ccgp-anhui', 'ccgp-hebei', 'ggzy.guizhou', 'zwfw.xinjiang',
                    'ggzyjy.gansu', 'jl.gov', 'hljcg', 'gdpepep', 'ebnew', 'vip', 'hunan.zcygov.cn', 'gdgpo.czt.gd',
                    'gd-prod.oss-cn-beijing.aliyuncs.com', 'zjcs.gdggzy.org.cn', 'ec.chng.com.cn', 'obei.com.cn',
                    'ccgp-xinjiang.gov.cn', 'ec.mcc.com.cn', 'jswater.jiangsu.gov.cn'],
        'annual_report': 'annual_report',
        # ['annual_report_1997', 'annual_report_1998', 'annual_report_1999', 'annual_report_2000', 'annual_report_2001', 'annual_report_2002', 'annual_report_2003', 'annual_report_2004', 'annual_report_2005', 'annual_report_2006', 'annual_report_2007', 'annual_report_2008', 'annual_report_2009', 'annual_report_2010', 'annual_report_2011', 'annual_report_2012', 'annual_report_2013', 'annual_report_2014', 'annual_report_2015', 'annual_report_2016', 'annual_report_2017', 'annual_report_2018', 'annual_report_2019', 'annual_report_2020', 'annual_report_2021'],
        'gw': 'tungee_homepage',
        'zp': ["58guakao", "baiduzhaopin", "baidajob", "51job", "goodjob", "lagou", "neitui", "jobcn", "360guakao",
               "chinahr", "hunt007", "liepin", "job36", "zhaopin", "ganji", "528", "dajie", "jobui", "bole", "51rc",
               "zhilian", "zhipin", "58", "bendizhaopin", "98pz", "fengpintech", "jobeast", "yilan", "nowcoder",
               "78gk", "job1001"]

    }
    week_min, week_max = Utils.get_date_range(ndays=7)
    one_month_min, one_month_max = Utils.get_date_range(ndays=30)
    two_month_min, two_month_max = Utils.get_date_range(ndays=60)
    three_month_min, three_month_max = Utils.get_date_range(ndays=90)

    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        self.init_zy_bloom()
        # todo other col
        self.contact_col = self.db.p_col('contact')

    def init_zy_bloom(self):
        '''
        包含所有在营企业nameId
        '''
        file_name_ls = []
        for file_name in os.listdir('/mnt5/dsf'):
            if file_name.count('dsf_zy_qy_nameId_get_new') and file_name.count('dc'):
                file_name_ls.append(file_name)
        zy_file_name = '/mnt5/dsf/{}'.format(max(file_name_ls))

        self.zy_nameId_bloom = Utils.bloom_filter_fac()
        index = 0
        with open(zy_file_name, 'r') as f:
            self.log('正在读取{}\t{}'.format(zy_file_name, index))
            for line in f:
                line = line.strip()
                self.zy_nameId_bloom.add(line)
                index += 1
                if index % 10000 == 0:
                    self.log('正在读取{}\t{}'.format(zy_file_name, index))
                # if index > 10000:
                #     break
        self.log('在营企业共有{}'.format(index))

    def handle_one_mongo_data(self, record_dic, result_dic=None):
        reaL_create_time = record_dic.get('create_time')
        record_dic = Utils.decrypt_one_record(record_dic, need_decrypt=True, need_strf_time=True)
        # ["contact", "nameId", "name", "socialCreditCode", "regNumber", "contactType", "exportTime"]
        contact = record_dic.get('contact')
        contactType = record_dic.get('contactType')
        isBaiduContact = record_dic.get('isBaiduContact')
        nameId = record_dic.get('nameId')
        update_time = record_dic.get('import_update_time', '')
        create_time = record_dic.get('create_time', '')
        contactChannelList = record_dic.get('contactChannelList', [])
        contactSource = record_dic.get('contactSource', '').strip() or contactChannelList and contactChannelList[-1][
            'contactSource']
        result_dic[ResultConfig.all_table_count] += 1
        must_exists_values = [update_time, create_time, contactChannelList, reaL_create_time]  # 这些字段都必须是非空值
        must_has_one_exists_values = []  # 这些字段必须最少有一个是非空值
        if self.is_values_has_none(must_exists_values):
            self.write_error_f.write('not all exists: {}'.format(must_exists_values))
        elif self.is_values_all_none(must_has_one_exists_values):
            self.write_error_f.write('not any exists {}'.format(must_has_one_exists_values))
        else:
            # 是否是绝对 TODO 判断逻辑不严谨，后期改成先遍历以前的联系方式加入bloom
            abs_flag = len(contactChannelList) == 1
            zy_flag = nameId in self.zy_nameId_bloom
            if abs_flag:
                result_dic[ResultConfig.all_abs_add] += 1

            if isBaiduContact:
                source_key = 'baidu'
            elif contactSource in self.source_dic['qcc']:
                source_key = 'qcc'
            elif contactSource.startswith(self.source_dic['annual_report']):
                source_key = 'annual_report'
            elif contactSource.startswith(self.source_dic['gw']):
                source_key = 'gw'
            elif contactSource in self.source_dic['b2b']:
                source_key = 'b2b'
            elif contactSource in self.source_dic['bidding']:
                source_key = 'bidding'
            elif contactSource in self.source_dic['zp']:
                source_key = 'zp'
            else:
                source_key = 'other'
                # if contactSource:
                #     source_key = contactSource
                # else:
                #     source_key = contactChannelList[-1]['contactSource']

            if source_key not in result_dic['source_result_dic']:
                result_dic['source_result_dic'][source_key] = {
                    'table_add_count': 0, 'table_update_count': 0,
                    'mobile_add': 0, 'mobile_update': 0,
                    'mobile_abs_add': 0,
                    'zy_mobile_add': 0, 'zy_mobile_update': 0,
                    'zy_mobile_abs_add': 0,
                }
            if contactSource not in result_dic['source_result_dic'][source_key]:
                result_dic['source_result_dic'][source_key][contactSource] = {
                    'table_add_count': 0, 'table_update_count': 0,
                    'mobile_add': 0, 'mobile_update': 0,
                    'mobile_abs_add': 0,
                    'zy_mobile_add': 0, 'zy_mobile_update': 0,
                    'zy_mobile_abs_add': 0,
                }

            # 库内联系方式与映射企业数去重
            if contactType == 0:
                result_dic[ResultConfig.table_mobile_count] += 1
                result_dic[ResultConfig.table_mobile_count_dup].add(contact)
                # 以前存在返回True，否则False
                result_dic[ResultConfig.table_mobile_count_ent_dup].add(nameId)
                if zy_flag:
                    result_dic[ResultConfig.zy_enterprise_mobile_count] += 1
                if self.one_month_min <= update_time <= self.one_month_max:
                    result_dic[ResultConfig.one_month_mobile_update] += 1
                    result_dic[ResultConfig.one_month_mobile_update_dup].add(nameId)
                    result_dic[ResultConfig.two_month_mobile_update] += 1
                    result_dic[ResultConfig.two_month_mobile_update_dup].add(nameId)
                    result_dic[ResultConfig.three_month_mobile_update] += 1
                    result_dic[ResultConfig.three_month_mobile_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.one_month_zy_enterprise_mobile_update] += 1
                        result_dic[ResultConfig.two_month_zy_enterprise_mobile_update] += 1
                        result_dic[ResultConfig.three_month_zy_enterprise_mobile_update] += 1
                elif self.two_month_min <= update_time <= self.two_month_max:
                    result_dic[ResultConfig.two_month_mobile_update] += 1
                    result_dic[ResultConfig.two_month_mobile_update_dup].add(nameId)
                    result_dic[ResultConfig.three_month_mobile_update] += 1
                    result_dic[ResultConfig.three_month_mobile_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.two_month_zy_enterprise_mobile_update] += 1
                        result_dic[ResultConfig.three_month_zy_enterprise_mobile_update] += 1
                elif self.three_month_min <= update_time <= self.three_month_max:
                    result_dic[ResultConfig.three_month_mobile_update] += 1
                    result_dic[ResultConfig.three_month_mobile_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.three_month_zy_enterprise_mobile_update] += 1

                if self.week_min <= create_time < self.week_max:
                    result_dic[ResultConfig.all_add] += 1
                    result_dic[ResultConfig.mobile_add] += 1
                    result_dic[ResultConfig.mobile_add_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.week_zy_enterprise_mobile_add] += 1
                        result_dic['source_result_dic'][source_key]['zy_mobile_add'] += 1
                        result_dic['source_result_dic'][source_key][contactSource]['zy_mobile_add'] += 1
                    result_dic['source_result_dic'][source_key]['mobile_add'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['mobile_add'] += 1
                    result_dic['source_result_dic'][source_key]['table_add_count'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['table_add_count'] += 1
                    if abs_flag:
                        result_dic[ResultConfig.mobile_abs_add] += 1
                        result_dic['source_result_dic'][source_key]['mobile_abs_add'] += 1
                        result_dic['source_result_dic'][source_key][contactSource]['mobile_abs_add'] += 1
                        if self.judget_abs_add_ent(nameId, reaL_create_time):
                            result_dic[ResultConfig.mobile_abs_add_ent_dup].add(nameId)
                        if zy_flag:
                            result_dic[ResultConfig.week_zy_enterprise_mobile_abs_add].add(contact)
                            result_dic['source_result_dic'][source_key]['zy_mobile_abs_add'] += 1
                            result_dic['source_result_dic'][source_key][contactSource]['zy_mobile_abs_add'] += 1
                # 不是昨日更新 而是昨日更新数
                elif self.week_min <= update_time < self.week_max:
                    result_dic[ResultConfig.all_update] += 1
                    result_dic[ResultConfig.mobile_update] += 1
                    result_dic['source_result_dic'][source_key]['table_update_count'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['table_update_count'] += 1
                    result_dic[ResultConfig.mobile_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.week_zy_enterprise_mobile_update] += 1
                        result_dic['source_result_dic'][source_key]['zy_mobile_update'] += 1
                        result_dic['source_result_dic'][source_key][contactSource]['zy_mobile_update'] += 1
                    result_dic['source_result_dic'][source_key]['mobile_update'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['mobile_update'] += 1


            elif contactType == 1:
                result_dic[ResultConfig.table_phone_count] += 1
                result_dic[ResultConfig.table_phone_count_dup].add(contact)
                result_dic[ResultConfig.table_phone_count_ent_dup].add(nameId)
                if zy_flag:
                    result_dic[ResultConfig.zy_enterprise_phone_count] += 1
                if self.one_month_min <= update_time <= self.one_month_max:
                    result_dic[ResultConfig.one_month_phone_update] += 1
                    result_dic[ResultConfig.one_month_phone_update_dup].add(nameId)
                    result_dic[ResultConfig.two_month_phone_update] += 1
                    result_dic[ResultConfig.two_month_phone_update_dup].add(nameId)
                    result_dic[ResultConfig.three_month_phone_update] += 1
                    result_dic[ResultConfig.three_month_phone_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.one_month_zy_enterprise_phone_update] += 1
                        result_dic[ResultConfig.two_month_zy_enterprise_phone_update] += 1
                        result_dic[ResultConfig.three_month_zy_enterprise_phone_update] += 1
                elif self.two_month_min <= update_time <= self.two_month_max:
                    result_dic[ResultConfig.two_month_phone_update] += 1
                    result_dic[ResultConfig.two_month_phone_update_dup].add(nameId)
                    result_dic[ResultConfig.three_month_phone_update] += 1
                    result_dic[ResultConfig.three_month_phone_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.two_month_zy_enterprise_phone_update] += 1
                        result_dic[ResultConfig.three_month_zy_enterprise_phone_update] += 1
                elif self.three_month_min <= update_time <= self.three_month_max:
                    result_dic[ResultConfig.three_month_phone_update] += 1
                    result_dic[ResultConfig.three_month_phone_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.three_month_zy_enterprise_phone_update] += 1
                if self.week_min <= create_time < self.week_max:
                    result_dic[ResultConfig.all_add] += 1
                    result_dic[ResultConfig.phone_add] += 1
                    result_dic['source_result_dic'][source_key]['table_add_count'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['table_add_count'] += 1
                    result_dic[ResultConfig.phone_add_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.week_zy_enterprise_phone_add] += 1
                    if abs_flag:
                        result_dic[ResultConfig.phone_abs_add] += 1
                        if self.judget_abs_add_ent(nameId, reaL_create_time):
                            result_dic[ResultConfig.phone_abs_add_ent_dup].add(nameId)
                        if zy_flag:
                            result_dic[ResultConfig.week_zy_enterprise_phone_abs_add].add(contact)
                # 不是昨日更新 而是昨日更新数
                elif self.week_min <= update_time < self.week_max:
                    result_dic[ResultConfig.all_update] += 1
                    result_dic[ResultConfig.phone_update] += 1
                    result_dic['source_result_dic'][source_key]['table_update_count'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['table_update_count'] += 1
                    result_dic[ResultConfig.phone_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.week_zy_enterprise_phone_update] += 1
            elif contactType == 2:
                result_dic[ResultConfig.table_email_count] += 1
                result_dic[ResultConfig.table_email_count_dup].add(contact)
                result_dic[ResultConfig.table_email_count_ent_dup].add(nameId)
                if zy_flag:
                    result_dic[ResultConfig.zy_enterprise_email_count] += 1
                if self.one_month_min <= update_time <= self.one_month_max:
                    result_dic[ResultConfig.one_month_email_update] += 1
                    result_dic[ResultConfig.one_month_email_update_dup].add(nameId)
                    result_dic[ResultConfig.two_month_email_update] += 1
                    result_dic[ResultConfig.two_month_email_update_dup].add(nameId)
                    result_dic[ResultConfig.three_month_email_update] += 1
                    result_dic[ResultConfig.three_month_email_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.one_month_zy_enterprise_email_update] += 1
                        result_dic[ResultConfig.two_month_zy_enterprise_email_update] += 1
                        result_dic[ResultConfig.three_month_zy_enterprise_email_update] += 1
                elif self.two_month_min <= update_time <= self.two_month_max:
                    result_dic[ResultConfig.two_month_email_update] += 1
                    result_dic[ResultConfig.two_month_email_update_dup].add(nameId)
                    result_dic[ResultConfig.three_month_email_update] += 1
                    result_dic[ResultConfig.three_month_email_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.two_month_zy_enterprise_email_update] += 1
                        result_dic[ResultConfig.three_month_zy_enterprise_email_update] += 1
                elif self.three_month_min <= update_time <= self.three_month_max:
                    result_dic[ResultConfig.three_month_email_update] += 1
                    result_dic[ResultConfig.three_month_email_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.three_month_zy_enterprise_email_update] += 1
                if self.week_min <= create_time < self.week_max:
                    result_dic[ResultConfig.all_add] += 1
                    result_dic[ResultConfig.email_add] += 1
                    result_dic['source_result_dic'][source_key]['table_add_count'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['table_add_count'] += 1
                    result_dic[ResultConfig.email_add_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.week_zy_enterprise_email_add] += 1
                    if abs_flag:
                        result_dic[ResultConfig.email_abs_add] += 1
                        if self.judget_abs_add_ent(nameId, reaL_create_time):
                            result_dic[ResultConfig.email_abs_add_ent_dup].add(nameId)
                        if zy_flag:
                            result_dic[ResultConfig.week_zy_enterprise_email_abs_add].add(contact)
                # 不是昨日更新 而是昨日更新数
                elif self.week_min <= update_time < self.week_max:
                    result_dic[ResultConfig.all_update] += 1
                    result_dic[ResultConfig.email_update] += 1
                    result_dic['source_result_dic'][source_key]['table_update_count'] += 1
                    result_dic['source_result_dic'][source_key][contactSource]['table_update_count'] += 1
                    result_dic[ResultConfig.email_update_dup].add(nameId)
                    if zy_flag:
                        result_dic[ResultConfig.week_zy_enterprise_email_update] += 1
        return result_dic

    def judget_abs_add_ent(self, nameId, create_time):
        '''
        判断是否是新增的企业,以前该企业无联系方式，现在有了;
        规则：库内以前没有该企业存在
        '''
        count = self.contact_col.find({"nameId": nameId, "create_time": {"$lt": create_time}}).count()
        if count<1:
            return True

    def run(self):

        save_result_dic = {
            ResultConfig.all_table_count: 0,  # 库内联系方式总量(未去重)

            ResultConfig.all_update: 0,  # 周更新量
            ResultConfig.all_add: 0,  # 周新增量 (未去重)
            ResultConfig.all_abs_add: 0,  # 绝对新增(contactChannelList为1的)

            ResultConfig.mobile_update: 0,  # 手机更新数量
            ResultConfig.mobile_update_dup: Utils.bloom_filter_fac(),  # 去重后手机更新数量的企业数量
            ResultConfig.mobile_add: 0,  # (未去重)
            ResultConfig.mobile_add_dup: Utils.bloom_filter_fac(),
            ResultConfig.mobile_abs_add: 0,
            ResultConfig.mobile_abs_add_ent_dup: Utils.bloom_filter_fac(),  # 周绝对新增企业数量 todo 可能存在性能问题

            ResultConfig.phone_update: 0,
            ResultConfig.phone_update_dup: Utils.bloom_filter_fac(),
            ResultConfig.phone_add: 0,
            ResultConfig.phone_add_dup: Utils.bloom_filter_fac(),
            ResultConfig.phone_abs_add: 0,
            ResultConfig.phone_abs_add_ent_dup: Utils.bloom_filter_fac(),  # 周绝对新增企业数量 todo 可能存在性能问题

            ResultConfig.email_update: 0,
            ResultConfig.email_update_dup: Utils.bloom_filter_fac(),
            ResultConfig.email_add: 0,
            ResultConfig.email_add_dup: Utils.bloom_filter_fac(),
            ResultConfig.email_abs_add: 0,
            ResultConfig.email_abs_add_ent_dup: Utils.bloom_filter_fac(),
            # -------------新增--------
            # 可能存在手机号重复的情况，即多家企业拥有同一个手机号，以后再对手机号进行去重
            ResultConfig.one_month_mobile_update: 0,  # 近一个月手机更新数量
            ResultConfig.one_month_mobile_update_dup: Utils.bloom_filter_fac(),  # 近一个月手机更新数量映射企业去重数
            ResultConfig.one_month_phone_update: 0,
            ResultConfig.one_month_phone_update_dup: Utils.bloom_filter_fac(),
            ResultConfig.one_month_email_update: 0,
            ResultConfig.one_month_email_update_dup: Utils.bloom_filter_fac(),
            ResultConfig.two_month_mobile_update: 0,  # 近2个月手机更新数量
            ResultConfig.two_month_mobile_update_dup: Utils.bloom_filter_fac(),  # 近2个月手机更新数量映射企业去重数
            ResultConfig.two_month_phone_update: 0,
            ResultConfig.two_month_phone_update_dup: Utils.bloom_filter_fac(),
            ResultConfig.two_month_email_update: 0,
            ResultConfig.two_month_email_update_dup: Utils.bloom_filter_fac(),
            ResultConfig.three_month_mobile_update: 0,  # 近3个月手机更新数量 TODO
            ResultConfig.three_month_mobile_update_dup: Utils.bloom_filter_fac(),  # 近3个月手机更新数量映射企业去重数TODO
            ResultConfig.three_month_phone_update: 0,  # TODO
            ResultConfig.three_month_phone_update_dup: Utils.bloom_filter_fac(),  # TODO
            ResultConfig.three_month_email_update: 0,  # TODO
            ResultConfig.three_month_email_update_dup: Utils.bloom_filter_fac(),  # TODO
            # 在营总量统计
            ResultConfig.zy_enterprise_count: self.zy_nameId_bloom.count,  # 在营企业的数量
            ResultConfig.zy_enterprise_mobile_count: 0,  # 在营企业的手机条数数量（未去重）  TODO
            ResultConfig.week_zy_enterprise_mobile_update: 0,  # 在营企业的手机条数数量周更新
            ResultConfig.week_zy_enterprise_mobile_add: 0,  # 在营企业的手机数量周新增
            ResultConfig.week_zy_enterprise_mobile_abs_add: Utils.bloom_filter_fac(),  # 在营企业的手机数量周绝对新增
            ResultConfig.one_month_zy_enterprise_mobile_update: 0,  # 在营企业的手机数量近一个月更新
            ResultConfig.two_month_zy_enterprise_mobile_update: 0,
            ResultConfig.three_month_zy_enterprise_mobile_update: 0,
            ResultConfig.zy_enterprise_phone_count: 0,  # TODO
            ResultConfig.week_zy_enterprise_phone_update: 0,
            ResultConfig.week_zy_enterprise_phone_add: 0,
            ResultConfig.week_zy_enterprise_phone_abs_add: Utils.bloom_filter_fac(),
            ResultConfig.one_month_zy_enterprise_phone_update: 0,  # 在营企业的手机数量近一个月更新
            ResultConfig.two_month_zy_enterprise_phone_update: 0,
            ResultConfig.three_month_zy_enterprise_phone_update: 0,
            ResultConfig.zy_enterprise_email_count: 0,  # TODO
            ResultConfig.week_zy_enterprise_email_update: 0,
            ResultConfig.week_zy_enterprise_email_add: 0,
            ResultConfig.week_zy_enterprise_email_abs_add: Utils.bloom_filter_fac(),
            ResultConfig.one_month_zy_enterprise_email_update: 0,  # 在营企业的手机数量近一个月更新
            ResultConfig.two_month_zy_enterprise_email_update: 0,
            ResultConfig.three_month_zy_enterprise_email_update: 0,
            # 全表数量统计
            ResultConfig.table_mobile_count: 0,  # 库内手机条数总量contact TODO
            ResultConfig.table_mobile_count_dup: Utils.bloom_filter_fac(),  # 库内手机号总量去重 TODO
            ResultConfig.table_mobile_count_ent_dup: Utils.bloom_filter_fac(),  # 库内手机总量 对应的 去重企业数量 TODO
            ResultConfig.table_phone_count: 0,  # 库内固话条数总量  TODO
            ResultConfig.table_phone_count_dup: Utils.bloom_filter_fac(),  # 库内固话总量的去重  TODO
            ResultConfig.table_phone_count_ent_dup: Utils.bloom_filter_fac(),  # 库内固话总量的去重企业数量  TODO
            ResultConfig.table_email_count: 0,  # 库内邮箱条数总量 # TODO
            ResultConfig.table_email_count_dup: Utils.bloom_filter_fac(),  # 库内邮箱总量的去重企业数量 # TODO
            ResultConfig.table_email_count_ent_dup: Utils.bloom_filter_fac(),  # 库内邮箱总量的去重企业数量 # TODO

            'source_result_dic': {
                # 'qcc': {
                #     'mobile_add': 0,  # 企查查周新增手机
                #     'mobile_update': 0,  # 企查查周更新手机
                #     'mobile_abs_add': 0,  # 企查查周绝对新增手机
                #     'zy_mobile_add': 0,  # 在营企业新增手机
                #     'zy_mobile_update': 0,  # 在营企业更新手机
                #     'zy_mobile_abs_add': 0,  # 在营企业周绝对新增手机
                # },
                # 'annual_report': {
                #     'mobile_add': 0,
                #     'mobile_abs_add': 0,
                #     'zy_mobile_add': 0,
                #     'zy_mobile_abs_add': 0,
                # },
                # 'baidu': {
                #     'mobile_add': 0,
                #     'mobile_abs_add': 0,
                #     'zy_mobile_add': 0,
                #     'zy_mobile_abs_add': 0,
                # },
                # 'b2b': {
                #     'mobile_add': 0,
                #     'mobile_abs_add': 0,
                #     'zy_mobile_add': 0,
                #     'zy_mobile_abs_add': 0,
                # },
                # 'bidding': {
                #     'mobile_add': 0,
                #     'mobile_abs_add': 0,
                #     'zy_mobile_add': 0,
                #     'zy_mobile_abs_add': 0,
                # },
                # 'gw': {
                #     'mobile_add': 0,
                #     'mobile_abs_add': 0,
                #     'zy_mobile_add': 0,
                #     'zy_mobile_abs_add': 0,
                # },
                # 'zp': {
                #     'mobile_add': 0,
                #     'mobile_abs_add': 0,
                #     'zy_mobile_add': 0,
                #     'zy_mobile_abs_add': 0,
                # },
                # 'other_source_set': set()
            },

        }
        config = {
            MongoQueryConfig.col_name: 'contact',
            MongoQueryConfig.filter: {
                'contactType': {"$in": [0, 1, 2]},
            },
            MongoQueryConfig.projection: ["nameId", "isBaiduContact", "contact", "contactType", "contactSource",
                                          "import_update_time",
                                          "create_time", "contactChannelList"]
        }
        result_dic = self.get_data_from_mongo(config, save_result_dic)

        # 机器人相关
        url = RobotConfig.dsf_s2_web_hook_url
        title = RobotConfig.dsf_s2_title
        robot = DingtalkRobot(self.write_robot_f)
        mk_text = self.make_mk_text(result_dic)
        robot.main(url, title=title, mk_text=mk_text)

    def make_mk_text(self, result_dic):
        '''通过数据 拼接出mk_text'''
        result_dic = Utils.serializable_bloom(result_dic)
        mk_text = ''
        new_sort_ls = []
        for name in dir(ResultConfig):
            field = getattr(ResultConfig, name)
            if isinstance(field, Field):
                new_sort_ls.append(field)
        new_sort_ls.sort()
        for field in new_sort_ls:
            print(field.prioties)

            mk_text += '**{}：{}**  \n'.format(field.title,result_dic[field])

        for key, v in result_dic['source_result_dic'].items():
            mk_text += '''  
                **==========**  
                **来源：{}**  
                **周更新库内总量：{}**  
                **周新增库内总量：{}**  
                **周更新库内手机总量：{}**  
                **周新增库内手机总量：{}**  
                **周绝对新增库内手机总量：{}**  
                **在营企业手机周更新：{}**  
                **在营企业手机周新增：{}**  
                **在营企业手机周绝对新增：{}**  
                **==========**  
                '''.format(key, v['table_update_count'], v['table_add_count'],
                           v['mobile_update'], v['mobile_add'],
                           v['mobile_abs_add'], v['zy_mobile_update'],
                           v['zy_mobile_add'], v['zy_mobile_abs_add'],
                           )
        with open('周报统计', 'w') as f:
            f.write(mk_text)
        return mk_text


def run(abs_file_name):
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(abs_file_name):
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='1', minute='1', id='dd_run')
    scheduler.start()



if __name__ == '__main__':
    file_name = '联系方式周报'
    # py文件名+file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)

    run(abs_file_name)
    # scheduler_run(abs_file_name)
